<!DOCTYPE html>
<html lang="en">
<head>
    {{include file="lib/page_head" /}}
    <style>
        table th{word-wrap:break-word;word-wrap:break-word;word-break:break-all;font-size:12px!important;}
        table td{word-wrap:break-word;word-wrap:break-word;word-break:break-all;}
        #edit_info .form-beautify-select{display:block;width: 100%!important;}
        .form-group{margin-bottom: 5px!important;}
    </style>
</head>
<body >

<!-- Preloader -->
<div id="preloader" style="display: none;">
    <div id="status"><i class="fa fa-spinner fa-spin"></i></div>
</div>

<section>
    {{include file="lib/page_left" /}}
    <div class="mainpanel">

        {{include file="lib/page_header" /}}

        <div class="contentpanel" >
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="panel-btns">
                        <a href="" class="panel-close">×</a>
                        <a href="" class="minimize">−</a>
                    </div>
                    <h4 class="panel-title">配置列表</h4>
                </div>
                <div class="panel-body">
                    <div class="form-inline"  id="info_list">
                        <div class="btn-group">
                            <button class="btn btn-primary" type="button" onclick="edit_info(0)">增加</button>
                            <button class="btn btn-primary" type="button" onclick="location.href = location.href;">刷新</button>
                        </div>

                        <div class="layui-form-item" style="margin-top:10px;">
                            <form class="layui-form" id="search_form" action="" onsubmit="return false;">
                                <div class="form-group">
                                    <label >客户名称</label>
                                    <input class="form-control" name="flow_name"  autocomplete="off" type="text">

                                </div>


                                <div style="display: inline-block;clear: both;">
                                    <button class="btn btn-primary" onclick="search()">查询</button>
                                    <button class="btn btn-default" onclick="search_reset()">取消</button>
                                </div>
                            </form>

                        </div>
                    </div>

                </div>
            </div>
        </div><!-- contentpanel -->

    </div><!-- mainpanel -->
    {{include file="lib/page_right" /}}
</section>

<div style="display: none;" id="edit_info">
    <form class="layui-form" action="">
        <input name="id" value="0" type="hidden">

        <div class="form-group">
            <label class="col-sm-3 control-label">选择类别</label>
            <div class="col-sm-8">
                <select class="select5" name="pid" data- lay-ignore  >


                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">菜单名称</label>
            <div class="col-sm-8">
                <input type="text" name="name"  placeholder="菜单名称" class="form-control" >
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">目标链接</label>
            <div class="col-sm-8">
                <input type="text" name="link"  placeholder="目标链接" class="form-control" >
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">排序号</label>
            <div class="col-sm-8">
                <input type="text" name="sort" value="0" placeholder="排序号" class="form-control" >
            </div>
        </div>

    </form>
</div>

<script src="../js/jquery-1.11.1.min.js"></script>
<script src="../js/jquery-migrate-1.2.1.min.js"></script>
<script src="../js/jquery-ui-1.10.3.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="../js/jquery.sparkline.min.js"></script>
<script src="../js/toggles.min.js"></script>
<script src="../js/jquery.cookies.js"></script>
<script src="../js/jquery.tmpl.js"></script>
<script src="../js/custom.js"></script>
<script src="../layui/layui.js"></script>
<script src="../js/jquery.gritter.min.js"></script>
<script src="../js/form.beautify.js?t={{$version}}"></script>
<script src="../js/commen.js?t={{$version}}"></script>
<script src="../js/datatables.js?t={{$version}}"></script>
<link href="../css/ley.dialog.css" rel="stylesheet">
<script src="../js/ley.dialog.js?t={{$version}}"></script>
<script>
    function formatState (state) {
        if (!state.id) { return state.text; }
        var $state = $(
            '<span><span class="fa ' + state.element.value.toLowerCase() + '"></span> ' + state.text + '</span>'
        );
        return $state;
    };
    layui.use(['layer','laypage',"element"], function(){



        //全选
//        form.on('checkbox(allChoose)', function(data){
//            var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
//            child.each(function(index, item){
//                item.checked = data.elem.checked;
//            });
//            form.render('checkbox');
//        });
        $("#info_list").datatables({
            rows:1,
            idField: 'id',
            columns:[
                {field:'name',title:'菜单名称',width:200,formatter:function(value,row,index){
                    var text = "";
                    if (row.icon)
                    {
                        text += '<span class="fa ' + row.icon + '"></span>&nbsp;'
                    }
                    return '<div style="padding-left:'+row.level_child*20+'px">'+text+value+'</div>';
                }} ,
                {field:'id',title:'菜单编号',width:120} ,
                {field:'pid',title:'菜单父级编号',width:120} ,
                {field:'sort',title:'排序号',width:120} ,
                {field:'is_log',title:'是否记录日志',width:60,formatter:function(value,row,index){
                    if(!value) {
                        return '<a href="javascript:void 0;" onclick="set_is_log(this,'+row.id+',1)"><span class="fa fa-square-o"></span></a>';
                    }else if(value == 1){
                        return '<a href="javascript:void 0;" onclick="set_is_log(this,'+row.id+',0)"><span class="fa fa-check-square-o"></span></a>';
                    }
                    return value;

                },},
                {field:'link',title:'目标连接',width:300,
                    formatter:function(value,row,index){
                        if(!value) {
                            return "<font color='red'>没有设置目标连接</font>";
                        }
                        return value;
                    },
                },
                {field:'operation',title:'操作',width:30,formatter:function (value,row,index) {
                    return '<div class="btn-group"> ' +
                        '<a data-toggle="dropdown" class="dropdown-toggle"> ' +
                        '<i class="fa fa-cog"></i> ' +
                        '</a> ' +
                        '<ul role="menu" class="dropdown-menu pull-right"> ' +
                        '<li><a href="javascript:void 0;" onclick="add_info(\''+row.id+'\')">添加</a></li> ' +
                        '<li><a href="javascript:void 0;" onclick="edit_info('+row.id+')">编辑</a></li> ' +
                        '<li><a href="javascript:void 0;" onclick="del_info(this)">删除</a></li> ' +
                        '</ul> ' +
                        '</div>';
                }},
            ],
            ajax:{
                url:"/adminmenu/ajax_sys_menu_list",
                callback:function (msg) {
                    var options = "";
                    for(var i=0;i<msg.data.length;i++)
                    {
                        var text = "";
                        for(var f=0;f<msg.data[i].level_child;f++)
                        {
                            text += "&nbsp;&nbsp;";
                        }
                        options += '<option value="'+msg.data[i].id+'">&nbsp;&nbsp;'+text+msg.data[i].name+'</option>';
                    }
                    options = '<option value="0">顶级分类</option>'+options;
                    $("#edit_info select[name='pid']").html(options);
                    check_table_list_data(msg);
                    check_parent_height();

                }

            }

        });



    });


    function set_is_log(obj,id,is_log) {
        AjaxCommen("GET","/adminmenu/ajax_set_sys_menu_is_log",{id:id,is_log:is_log},function (msg) {
            if (!msg.error)
            {
                var text = "";
                if(!is_log) {
                    text = '<a href="javascript:void 0;" onclick="set_is_log(this,'+id+',1)"><span class="fa fa-square-o"></span></a>';
                }else if(is_log == 1){
                    text = '<a href="javascript:void 0;" onclick="set_is_log(this,'+id+',0)"><span class="fa fa-check-square-o"></span></a>';
                }
                $(obj).parent().html(text);
            }
        });
    }


    function add_info(id)
    {
        edit_info(0,id);

    }
    function edit_info(id,pid) {

        $("#edit_info form")[0].reset();
        $(".select5,.select6").trigger("change");
        $("#edit_info").find("input[name='id']").val(0);
        if(pid)
        {
            $("#edit_info select[name='pid']").val(pid).trigger("change");
        }
        if(id>0)
        {
            AjaxCommen("GET","/adminmenu/ajax_sys_menu_info",{id:id},function (msg) {
                if (!msg.error)
                {
                    var obj = $("#edit_info input,#edit_info select,#edit_info textarea");
                    for(var i=0;i<obj.length;i++)
                    {
                        var name = obj.eq(i).attr("name");
                        if(msg.data[name])
                        {
                            obj.eq(i).val(msg.data[name]);
                            if(obj.eq(i).is("select"))
                            {
                                obj.eq(i).trigger("change");
                            }
                        }
                    }
                    open_info();
                }
            });
        }else{
            open_info();
        }

    }

    function get_search_data()
    {
        var data = {};

        var agent_name = $("#search_form input[name='agent_name']").val();

        data["agent_name"] = agent_name;


        return data;
    }
    function search()
    {
        var data = get_search_data();
        console.log(data)
        $("#info_list").setParems(data);
        $("#info_list").reload();
    }
    function search_reset() {
        $("#search_form")[0].reset();
        search();
    }
    function open_info()
    {

        openDialog2({
            title:"信息",
            area: ['600px;',"300px"],
            content:$("#edit_info"),
            clone:true,
            id:"edit_info_list",
            yes:function (index,layero) {
                var info = {};
                var obj = $("#edit_info input,#edit_info select,#edit_info textarea",layero);
                for(var i=0;i<obj.length;i++)
                {
                    var name = obj.eq(i).attr("name");
                    if(name)
                    {
                        info[name] = obj.eq(i).val();
                    }


                }

                if(info.name.length<1)
                {
                    openMsg("名称不能为空");
                    return false;
                }
                if(isNaN(info.sort))
                {
                    openMsg("排序号必须是数字");
                    return false;
                }

                AjaxCommen("POST","/adminmenu/ajax_sys_menu_save",info,function (msg) {
                    if (!msg.error)
                    {
                        openMsg("成功");
                        layero.close();
                        search();
                    }
                });


            }
        });
    }
    function del_info(obj)
    {
        var row_list = $("#info_list").getRows(obj);
        if(row_list.length < 1)
        {
            openMsg("请选择一条记录编辑");
            return false;
        }
        delMsg(function () {
            AjaxCommen("POST","/adminmenu/ajax_sys_menu_del",{row_list:row_list},function (msg) {
                if (!msg.error)
                {
                    openMsg("成功");
                    search();
                }
            });
        });
    }

</script>

</body>
</html>